package com.woniushop.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.woniushop.entity.GoodsType;
import com.woniushop.entity.PageBean;
import com.woniushop.entity.User;
import com.woniushop.service.GoodsTypeService;
import com.woniushop.service.imple.GoodsTypeServiceImple;
import com.woniushop.utils.ControllerUtils;

/**
 * Servlet implementation class GoodsTypeServlet
 */
@WebServlet("/goodsType")
public class GoodsTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private GoodsTypeService goodsTypeService = new GoodsTypeServiceImple();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String opr = request.getParameter("opr");
		switch (opr) {
		case "checkGtname":// 检查商品类别是否存在
			checkGtname(request, response);
			break;
		case "add":// 增加商品类别
			add(request, response);
			break;
		case "list":// 分页查询所有商品类别
			list(request, response);
			break;
		case "changeGtstate":// 改变商品类别状态
			changeGtstate(request, response);
			break;
		case "initUpdate":// 初始化修改页面
			initUpdate(request, response);
			break;
		case "update":// 修改商品类型
			update(request, response);
			break;
		case "findAllValid":// 获得所有商品类型
			findAllValid(request, response);
			break;
		default:

			break;
		}
	}

	/**
	 * 获得所有可用的商品类型
	 * 
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void findAllValid(HttpServletRequest request, HttpServletResponse response)
			throws JsonGenerationException, JsonMappingException, IOException {
		// 创建map用来传递JSON数据
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			GoodsType goodsType = new GoodsType();
			goodsType.setGtstate(1);
			// 获得所有数据
			List<GoodsType> list = goodsTypeService.findAll(goodsType);
			map.put("list", list);
			map.put("msg", "ok");
		} catch (Exception e) {
			map.put("msg", "系统维护中，请稍后再试！");
			e.printStackTrace();
		}

		// 返回JSON
		ControllerUtils.returnJson(map, response);
	}

	/**
	 * 修改商品类型
	 * 
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws JsonGenerationException, JsonMappingException, IOException {
		// 创建map用来传递JSON数据
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			// 获得登录用户
			User loginUser = (User) request.getSession().getAttribute("loginUser");
			if (loginUser == null || loginUser.getUstate() == 2 || loginUser.getUrole() == 3) {
				map.put("msg", "null");
			} else {
				// 获得用户对象
				Map<String, String[]> parameterMap = request.getParameterMap();
				GoodsType goodsType = (GoodsType) ControllerUtils.mapToObject(parameterMap, GoodsType.class);

				// 判断商品类别是否存在
				if (goodsType != null && goodsTypeService.checkGtname(goodsType.getGtname())) {
					// 设置创建人id
					goodsType.setUid(loginUser.getUid());
					goodsTypeService.update(goodsType);
					map.put("msg", "ok");
				} else {
					map.put("msg", "used");
				}
			}

		} catch (

		Exception e) {
			map.put("msg", "系统维护中，请稍后再试！");
			e.printStackTrace();
		}

		// 返回JSON
		ControllerUtils.returnJson(map, response);
	}

	/**
	 * 初始化修改页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void initUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获得gtid
			Integer gtid = Integer.parseInt(request.getParameter("gtid"));
			// 根据gtid查找商品类别
			GoodsType goodsType = goodsTypeService.findOne(gtid);
			if (goodsType == null) {
				request.setAttribute("msg", "该商品类型不存在，请刷新后重试。");
			} else {
				// 将商品类别存入请求作用域
				request.setAttribute("goodsType", goodsType);
			}
		} catch (Exception e) {
			request.setAttribute("msg", "页面出错，请稍后再试！");
		}
		request.getRequestDispatcher("manage/goodsTypeUpdate.jsp").forward(request, response);
	}

	/**
	 * 改变商品类别状态
	 * 
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void changeGtstate(HttpServletRequest request, HttpServletResponse response)
			throws JsonGenerationException, JsonMappingException, IOException {
		// 创建map用来传递JSON数据
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			// 获得登录用户
			User loginUser = (User) request.getSession().getAttribute("loginUser");
			if (loginUser == null || loginUser.getUstate() == 2 || loginUser.getUrole() == 3) {
				map.put("msg", "null");
			} else {
				// 获得商品类别id
				Integer gtid = Integer.parseInt(request.getParameter("gtid"));
				// 获得商品类别目标状态
				Integer gtstate = Integer.parseInt(request.getParameter("gtstate"));
				// 修改商品类别状态
				goodsTypeService.changeGtstate(gtid, gtstate);
				map.put("msg", "ok");
			}
		} catch (Exception e) {
			map.put("msg", "系统维护中，请稍后再试！");
			e.printStackTrace();
		}

		// 返回JSON
		ControllerUtils.returnJson(map, response);
	}

	/**
	 * 分页查询所有商品类别
	 * 
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws JsonGenerationException, JsonMappingException, IOException {
		// 创建map用来传递JSON数据
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			// 创建pageBean
			PageBean pageBean = new PageBean();

			// 获得页码，如果没有页码，则设置页码为1
			String page = request.getParameter("page");
			if (page == null || page.trim().length() == 0 || Integer.parseInt(page) < 1) {
				pageBean.setPage(1);
			} else {
				pageBean.setPage(Integer.parseInt(page));
			}

			// 设置分页大小
			pageBean.setPageSize(5);

			// 获得分页数据
			List<GoodsType> list = goodsTypeService.findByPage(pageBean, new GoodsType());
			map.put("list", list);
			map.put("pageBean", pageBean);
			map.put("msg", "ok");
		} catch (Exception e) {
			map.put("msg", "系统维护中，请稍后再试！");
			e.printStackTrace();
		}

		// 返回JSON
		ControllerUtils.returnJson(map, response);
	}

	/**
	 * 增加商品类别
	 * 
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws JsonGenerationException, JsonMappingException, IOException {
		// 创建map用来传递JSON数据
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			// 获得登录用户
			User loginUser = (User) request.getSession().getAttribute("loginUser");
			if (loginUser == null || loginUser.getUstate() == 2 || loginUser.getUrole() == 3) {
				map.put("msg", "null");
			} else {
				// 获得用户对象
				Map<String, String[]> parameterMap = request.getParameterMap();
				GoodsType goodsType = (GoodsType) ControllerUtils.mapToObject(parameterMap, GoodsType.class);

				// 判断商品类别是否存在
				if (goodsType != null && goodsTypeService.checkGtname(goodsType.getGtname())) {
					// 设置创建人id
					goodsType.setUid(loginUser.getUid());
					goodsTypeService.add(goodsType);
					map.put("msg", "ok");
				} else {
					map.put("msg", "used");
				}
			}

		} catch (

		Exception e) {
			map.put("msg", "系统维护中，请稍后再试！");
			e.printStackTrace();
		}

		// 返回JSON
		ControllerUtils.returnJson(map, response);
	}

	/**
	 * 检查商品类别是否存在
	 * 
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void checkGtname(HttpServletRequest request, HttpServletResponse response)
			throws JsonGenerationException, JsonMappingException, IOException {
		// 创建map用来传递JSON数据
		Map<String, Object> map = new HashMap<String, Object>();

		try {
			// 获得商品类别名
			String gtname = request.getParameter("gtname");
			// 判断该商品类别是否存在
			if (gtname != null && goodsTypeService.checkGtname(gtname)) {
				map.put("msg", "ok");
			} else {
				map.put("msg", "used");
			}
		} catch (Exception e) {
			map.put("msg", "系统维护中，请稍后再试！");
			e.printStackTrace();
		}

		// 返回JSON
		ControllerUtils.returnJson(map, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
